export default {
    data() {
        return {
            type: 1,
            page: 1,
            limit: 10,
            count: 0,
            tableData: [], // 表格数据
            name: '',
            // 日期选择
            date: '',
        }
    },

    /**
     * 进入页面加载
     */
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        that.getList();
    },

    /**
     * 方法
     */
    methods: {
        /**
         * 获取列表
         */
        getList: function () {
            var that = this;
            that.tableData = [];
            that.count = 0;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.type = that.type;
            //时间条件
            if (that.date) {
                formData.create_time = that.formatDateTime(that.date[0]);
                formData.last_time = that.formatDateTime(that.date[1]);
            }
            if (that.name) {
                formData.keys = that.name;
            }
            that.$http.post(that.adminApi.api_url + "/Weekend/index", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body.list) {
                        for (var i in res.body.list) {
                            if (res.body.list[i].is_submit === 1) {
                                if (res.body.list[i].status === 1) {
                                    var text = '已上线';
                                }
                                if (res.body.list[i].status === 2) {
                                    var text = '已下线';
                                }
                            } else {
                                var text = '未提交';
                            }
                            if (res.body.list[i].is_end === 1) {
                                var end_text = "<span style='color: #67C23A;'>完成</span>";
                            } else {
                                var end_text = "<span style='color: #F56C6C;'>未完成</span>";
                            }
                            if(res.body.list[i].stage){
                              var stage = res.body.list[i].stage;
                            }else {
                                var stage = '--';
                            }
                            that.tableData.push({
                                i: i,
                                id: res.body.list[i].goods_id,
                                img: res.body.list[i].picture[0],
                                text: res.body.list[i].title,
                                prcie: res.body.list[i].cost_price,
                                sn: res.body.list[i].sn,
                                add_time: res.body.list[i].create_time,
                                submit_time: res.body.list[i].submit_time,
                                status: text,
                                is_end: res.body.list[i].is_end,
                                is_submit: res.body.list[i].is_submit,
                                stage: stage,
                                end_text: end_text,
                            });
                        }
                        that.count = res.body.count;
                    }
                });
        },

        /**
         * 筛选
         */
        searchList: function () {

            console.log(this.date)
            let that = this;
            that.getList();
        },

        /**
         * 选择类型
         * @param tab
         */
        checkType(tab) {
            let that = this;
            that.type = tab;
            that.getList();
        },

        /**
         * 添加
         */
        add_weekend() {
            this.$router.push('/Weekend/add_weekend_list')
        },

        /**
         * 下一页
         * @param currentPage
         */
        handleCurrentChange: function (currentPage) {
            var that = this;
            that.page = currentPage;
        },


        /**
         * 时间转换
         * @param date
         * @returns {string}
         */
        formatDateTime: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            var minute = date.getMinutes();
            minute = minute < 10 ? ('0' + minute) : minute;
            return y + '-' + m + '-' + d ;
        },


        /**
         * 修改
         */
        edit: function (e) {
            let that = this;
            console.log(e);
            that.$router.push({
                path: '/Weekend/add_weekend_list',
                query: {
                    goods_id: e
                }
            });
        },


        /**
         * 删除
         */
        delGoods: function (e) {
            var that = this;
            that.$confirm('您却定删除该产品吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.doDelete(e);
            })
        },
        //执行删除
        doDelete: function (e) {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/Weekend/goodsdel", {
                token: that.token,
                goods_id: e,
                seller_id: that.user_id
            }, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '删除成功' }`
                    });
                    that.getList();
                },
                function (res) {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ res.body.msg }`
                    });
                });
        },
    }
}